<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Pelanggan_Model extends CI_Model {
	var $table_name = 'pelanggan';
	
    var $column_PK = "id";
    var $PK_array = array(
            "id" => "",
            );
    var $column_array = array(
            "nama" => "",
            "alamat" => "",
            "telepon" => "",
            "email" => "",
            "deskripsi" => "",
            );
    var $order_by_array = array(
            "id" => "asc",
            );
    
    private function process_data_to_DB($column, $data)
    {
        // if ($column == "nama")
            // $data = $data;
        
        return $data;
    }
    
	public function __construct() {
		parent::__construct();
        
        foreach($this->PK_array as $PK_name => $PK_value)
        {
            $this->PK_array[$PK_name] = $this->input->post($PK_name);
        }
        
        foreach($this->column_array as $column_name => $column_value)
        {
            $this->column_array[$column_name] = $this->input->post($column_name);
        }
	}
    
    public function get($limit=false, $pagenumber=1) // limit false = get all, limit int = get one or more row
    {
        if ($limit !== false)
        {
            $this->do_where_PK(false);
        }
        
        $this->do_order_by();
        
        if ($limit)
            $query = $this->db->get($this->table_name, $limit, ($pagenumber-1) * $limit);
        else
            $query = $this->db->get($this->table_name);
        
        return $query->result();
    }
    
    public function save() //insert or update
    {
        $this->do_where_PK();
        $is_update = ($this->db->get($this->table_name, 1, 0)->num_rows() == 1)?true:false;
        //echo "IS UPDATE : ".($is_update?'yes':'no');
        foreach ($this->column_array as $column_name => $column_value)
        {
            if ($column_value !== false)
                $this->db->set($column_name, $column_value);
            //echo $column." : ".$this->input->post($column)."<br/>";
        }
        
        if ($is_update) //update
        {
            $this->do_where_PK();
            return $this->db->update($this->table_name);
        }
        else //insert
        {
            $this->db->insert($this->table_name);
            return $this->db->insert_id();
        }
    }
    
    public function delete()
    {
        $this->do_where_PK();
        $this->db->delete($this->table_name); 
    }
    
    private function do_where_PK($strict=true) //strict if all PK must be set
    {
        foreach ($this->PK_array as $PK_name => $PK_value)
        {
            if ($strict)
            {
                $this->db->where($PK_name, $PK_value);
            }
            else
            {
                if ($PK_value !== false)
                    $this->db->where($PK_name, $PK_value);
            }
        }
    }
    
    private function do_order_by()
    {
        foreach ($this->order_by_array as $order_by => $order_rule)
            $this->db->order_by($order_by, $order_rule);
    }
}
?>